package com.qcit.common.view;

import android.content.Context
import android.graphics.drawable.Drawable
import android.os.Build
import android.text.InputType
import android.text.method.PasswordTransformationMethod
import android.util.AttributeSet
import android.view.View
import android.widget.EditText
import android.widget.ImageView
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.annotation.RequiresApi
import com.qcit.common.R
import com.qcit.user.bean.common.empty

class InputMenuView : RelativeLayout {
    val tv_title: TextView
    val ed_content: EditText
    val iv_image: ImageView
    val iv_icon: ImageView
    var moreListener: OnMoreClickListener? = null
    var titletext: String? = null
    var hinttext: String? = "请选择"
    var inputType:Int
    var moreShown: Boolean = true
    var edtext: String? = ""
    var edit_able: Boolean = true
    var icon_value: Drawable? = null

    var password:Boolean =false
    var external_data: Any? = null
        set(value) {field=value}
        get() {return field}

    @RequiresApi(Build.VERSION_CODES.O)
    constructor(mContext: Context, attrs: AttributeSet) : super(
        mContext,
        attrs
    ) {
        val inflate = inflate(context, R.layout.view_menu_input, this)
        tv_title = inflate.findViewById(R.id.tv_title)
        ed_content = inflate.findViewById(R.id.ed_content)
        iv_image = inflate.findViewById(R.id.iv_image)
        iv_icon = inflate.findViewById(R.id.iv_icon)
        iv_image.setOnClickListener { moreListener?.let { moreListener!!.OnMoreClicked(this) } }
        ed_content.setOnClickListener {
            if (ed_content.focusable == NOT_FOCUSABLE) {
                moreListener?.let { moreListener!!.OnMoreClicked(this) }
            }
        }
        val a = mContext.obtainStyledAttributes(attrs, R.styleable.InputMenuView)
        titletext = a.getString(R.styleable.InputMenuView_title)
        icon_value = a.getDrawable(R.styleable.InputMenuView_icon)
        password = a.getBoolean(R.styleable.InputMenuView_password,password)
        hinttext = a.getString(R.styleable.InputMenuView_hint)
        inputType = a.getInt(R.styleable.InputMenuView_inputType,InputType.TYPE_CLASS_TEXT)
        edtext = a.getString(R.styleable.InputMenuView_text)
        moreShown = a.getBoolean(R.styleable.InputMenuView_showMoreButton, true)
        edit_able = a.getBoolean(R.styleable.InputMenuView_editable, true)
        a.recycle()

        if (titletext.empty()) {
            tv_title.visibility = View.GONE
        } else {
            tv_title.setText(titletext)
        }
        icon_value?.let {
            iv_icon.visibility = View.VISIBLE
            iv_icon.setImageDrawable(it)
        } ?: { iv_icon.visibility = View.GONE }
        ed_content.setHint(hinttext)
        ed_content.setText(edtext)
        if (edit_able) {
            ed_content.focusable = FOCUSABLE
        } else {
            ed_content.focusable = NOT_FOCUSABLE
        }
        if (moreShown) {
            iv_image.visibility = View.VISIBLE
        } else {
            iv_image.visibility = View.GONE
        }
        if(password){
            ed_content.setTransformationMethod(PasswordTransformationMethod.getInstance());
        }
        ed_content.inputType = inputType
    }

    @RequiresApi(Build.VERSION_CODES.O)
    public fun setEditable(editable: Boolean) {
        edit_able = editable
        if (edit_able) {
            ed_content.focusable = FOCUSABLE
        } else {
            ed_content.focusable = NOT_FOCUSABLE
        }
    }

    public fun getText(): String {
        return ed_content.text.toString()
    }

    public fun setText(text: String?) {
        edtext = text
        ed_content.setText(edtext)
    }

    public fun setTitle(title: String?) {
        titletext = title
        tv_title.setText(titletext)
    }

    public fun setHint(hint: String?) {
        hinttext = hint
        ed_content.setHint(hinttext)
    }

    public fun setShowMore(show: Boolean) {
        this.moreShown = show
        if (moreShown) {
            iv_image.visibility = View.VISIBLE
        } else {
            iv_image.visibility = View.GONE
        }
    }

    public fun setOnMoreClickListener(onMoreClickListener: OnMoreClickListener) {
        moreListener = onMoreClickListener
    }

    interface OnMoreClickListener {
        public fun OnMoreClicked(menuview: InputMenuView);
    }
}